package jsyf.oa.web.customerReport;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportCustomerExcel;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.customerReport.AllCustomerReportFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 92852 on 2018/8/11.
 */
@Api(value = "客户报表接口", description = "客户报表接口")
@Controller
@RequestMapping("/allCustomerReport")
public class AllCustomerReportController {
    @Resource
    private AllCustomerReportFacade allCustomerReportFacade;
    @ResponseBody
    @RequestMapping(value = "/getCustomersByUserIds.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取全部客户",
            notes = "获取全部客户",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userIds:'',key:'',isIntention:''}", dataType = "String", paramType = "body")
    public void getCustomersByUserIds(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObjectList = allCustomerReportFacade.getCustomersByUserIds(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(jsonObjectList));
    }

    @RequestMapping(value = "/getDownByCondition.json", method = RequestMethod.GET)
    public void getDownByCondition(HttpServletResponse response,@RequestParam String userIds,@RequestParam String key,@RequestParam String isIntention) throws Exception {
        //查出来的数据
        List<JSONObject> jsonObjectList =null;
        jsonObjectList = allCustomerReportFacade.getOutGroupByUserId(userIds,key,isIntention);
        getDownExcel(jsonObjectList, response);
    }
    public void getDownExcel(List<JSONObject> jsonObjectList, HttpServletResponse response) throws Exception {
        //导出表名、列名
        String title = "客户信息列表";
        String[] rowsName = new String[]{ "区域经理姓名","区域经理电话","客户姓名", "电话", "类型", "信用", "生日", "微信","所在省", "所在市","所在县/区", "现有设备"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < jsonObjectList.size(); i++) {
            JSONObject jsonObject = jsonObjectList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = jsonObject.get("createUserName");
            objs[1] = jsonObject.get("createUserTel");
            objs[2] = jsonObject.get("customerName");
            objs[3] = jsonObject.get("tel");
            objs[4] = jsonObject.get("customerType");
            objs[5] = jsonObject.get("customerCredit");
            objs[6] = jsonObject.get("birthday");
            objs[7] = jsonObject.get("wechat");
            objs[8] = jsonObject.get("provinceName");
            objs[9] = jsonObject.get("cityName");
            objs[10] = jsonObject.get("countyName");
            objs[11] = jsonObject.get("existingDevice");
            dataList.add(objs);
        }
        ExportCustomerExcel ex = new ExportCustomerExcel(title, rowsName, dataList, response);
        ex.export();
    }

    @ResponseBody
    @RequestMapping(value = "/updateCustomerCreateByUserId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改客户创建者",
            notes = "修改客户创建者",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:'',regionIds:'',}", dataType = "String", paramType = "body")
    public void updateCustomerCreateByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        String results = allCustomerReportFacade.updateCustomerCreateByUserId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(results));
    }

    @ResponseBody
    @RequestMapping(value = "/updateProvinceById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改客户所在地或客户创建人",
            notes = "修改客户所在地或客户创建人",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{customerIds:'',userIds:'',type:'',key:'',provinceName:'',cityName:'',countyName:'',isCreate:'',customerCreateUserId:''}", dataType = "String", paramType = "body")
    public void updateProvinceById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject results = allCustomerReportFacade.updateProvinceById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(results));
    }
    
    @ResponseBody
    @RequestMapping(value = "/reportDayTree.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取树",
            notes = "获取树",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:''}", dataType = "String", paramType = "body")
    public void reportDayTree(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> results = allCustomerReportFacade.reportDayTree(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrHourTimeRespond(results));
    }

    @ResponseBody
    @RequestMapping(value = "/updateCreateUser.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改客户创建人",
            notes = "修改客户创建人",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{customerCreateUserId:''}", dataType = "String", paramType = "body")
    public void updateCreateUser(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject results = allCustomerReportFacade.updateCreateUser(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(results));
    }





}
